package cn.getech.data.development.controller.api;

import cn.getech.data.development.api.JobInfoApi;
import cn.getech.data.development.constant.LastRunState;
import cn.getech.data.development.dto.JobInfoDto;
import cn.getech.data.development.entity.JobInfo;
import cn.getech.data.development.service.JobInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @ClassName JobInfoApiController
 * @Description TODO
 * @Author Getech
 * @Date 2020/12/24 11:46
 */
@RestController
public class JobInfoApiController implements JobInfoApi {

    @Autowired
    private JobInfoService jobInfoService;

    @Override
    public JobInfo getJobInfoById(Integer jobInfoId) {
        return jobInfoService.getById(jobInfoId);
    }

    @Override
    public void updateJobInfoById(JobInfo jobInfo) {
        jobInfoService.updateById(jobInfo);
    }

    @Override
    public List<JobInfoDto> selectListByIds(Set<Integer> jobIds) {
        return jobInfoService.selectListByIds(jobIds);
    }

    @Override
    public List<JobInfo> selectListByStatus(Integer status) {
        return jobInfoService.list(new QueryWrapper<JobInfo>().eq("last_run_state",status));
    }

    @Override
    public void updateJobInfoAndStatusById(JobInfo jobInfo) {
        //先查询对应的任务数据（然后处理对应的停止状态）
        JobInfo jobInfo1 = jobInfoService.getById(jobInfo.getId());
        if(null != jobInfo1 && Objects.equals(jobInfo1.getLastRunState(), LastRunState.STOP.getCode())){
            jobInfo.setLastRunState(LastRunState.STOP.getCode());
        }
        jobInfoService.updateById(jobInfo);
    }
}
